package com.kzd.btgame.main.home

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.palette.graphics.Palette
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.kzd.btgame.R
import com.kzd.btgame.custom.setTransitionStatusT
import com.kzd.btgame.databinding.ActivityHomeDoingsBinding
import com.kzd.btgame.entity.Doings
import com.kzd.btgame.entity.DoingsTop
import com.kzd.btgame.entity.GameDoing
import com.kzd.btgame.jetpack.vm.HomeViewModel
import com.kzd.btgame.main.KEY_DOINGS_DOING
import com.kzd.btgame.main.KEY_DOINGS_TITLE
import com.kzd.btgame.main.KEY_DOINGS_TYPE
import com.kzd.btgame.main.game.adapter.Game1Adapter
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadImage
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class HomeDoingsActivity : OwnerViewBindingActivity<ActivityHomeDoingsBinding>() {

    private val mDoingTitle: String? by lazy {
        intent?.extras?.getString(KEY_DOINGS_TITLE)
    }

    private val mDoingMark: String? by lazy {
        intent?.extras?.getString(KEY_DOINGS_TYPE)
    }

    private val mDoingDoing: Doings<GameDoing>? by lazy {
        intent?.extras?.getSerializable(KEY_DOINGS_DOING) as? Doings<GameDoing>
    }

    private var mStart: Long = 0

    private val mGame1Adapter: Game1Adapter<GameDoing> by lazy {
        Game1Adapter()
    }

    private val mViewModel: HomeViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityHomeDoingsBinding
        get() = ActivityHomeDoingsBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flHomeDoingsBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.flHomeDoingsBar.setTitleTextContent(mDoingTitle)

        mViewBinding.includeHoneDoingsBody.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mGame1Adapter

        mViewBinding.includeHoneDoingsBody.mSmartRefreshLayout.setOnRefreshListener {
            val layout = mDoingMark
                ?: return@setOnRefreshListener
            mViewModel.homeDoings(layout)
        }
        mViewBinding.includeHoneDoingsBody.mSmartRefreshLayout.setOnLoadMoreListener {
            val layout = mDoingMark
                ?: return@setOnLoadMoreListener
            mViewModel.homeDoings(layout, mStart, 2, (true))
        }
    }

    override fun initData() {
        if (mDoingDoing == null) mDoingMark?.let {
            mViewModel.homeDoings(it)
        } else mDoingDoing?.let {
            fillData(it)
        }
    }

    override fun initViewMode() {
        mViewModel.homeDoingsLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusT(response,
                mGame1Adapter,
                mViewBinding.includeHoneDoingsBody.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            mStart = data.start
            if (isLoadMore) {
                mGame1Adapter.addData(data.list)
            } else {
                fillData(data)
            }
        })
    }

    private fun fillData(doing: Doings<GameDoing>) {
        mGame1Adapter.setHeaderView(createHeaderView(doing.top))
        mViewBinding.clHomeDoingsBack.setBackgroundColor(Color.parseColor(doing.top.color))
//        setOnPageChangeHeader(clHomeDoingsBack, "")
        mGame1Adapter.setNewInstance(doing.list)
        mStart = doing.start
    }

    private fun createHeaderView(top: DoingsTop): View {
        val view = LayoutInflater.from(this as Context)
            .inflate(R.layout.item_doings_header, null)
        view.findViewById<TextView>(R.id.tvDoingsHeaderContent)
            .text = top.content
        view.findViewById<ImageView>(R.id.ivDoingsHeaderImage)
            .loadImage(top.image)
        return view
    }


    private fun setOnPageChangeHeader(view: View, image: String) =
        Glide.with(this).asBitmap().load(image).into(object : CustomTarget<Bitmap>() {
            override fun onResourceReady(
                resource: Bitmap, transition: Transition<in Bitmap>?,
            ) {
                setPalette(resource)
            }

            override fun onLoadCleared(placeholder: Drawable?) = Unit

            fun setPalette(resource: Bitmap) = runOnUiThread {
                Palette.from(resource).generate {
                    val colorsArray = intArrayOf(
                        it?.lightMutedSwatch?.rgb ?: 0, it?.lightMutedSwatch?.rgb ?: 0
                    )
                    val drawable = GradientDrawable().apply {
                        orientation = GradientDrawable.Orientation.TOP_BOTTOM
                        colors = colorsArray
                    }
                    view.background = drawable
                }
            }
        })
}